package com.fishingwithme.infrastructure.dtos;

import com.fishingwithme.domain.spots.FishType;
import com.fishingwithme.domain.spots.HarvestType;
import com.fishingwithme.domain.spots.ScoreType;
import com.fishingwithme.infrastructure.utils.CommonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import org.assertj.core.util.Lists;
import org.json.JSONException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Data
public class VisitHarvestDto {
    private Long id;
    private Long visitId;
    private Long spotId;
    private String jsonPictures;
    private List<String> pictures;

    private String jsonFishHarvests;

    private Boolean isRec;
    private HarvestType harvestType;
    private ScoreType score;
    private Integer scoreValue;
    private String comment;
    private String cityCode;

    private List<FishType> fishes;


    public void format() {
        try {
            List<JsonNode> nodes = Optional.ofNullable(this.fishes).orElse(Lists.newArrayList()).stream().map(dto -> {
                try {
                    return dto.toJson();
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }).collect(Collectors.toList());
            this.jsonFishHarvests = CommonUtils.objectMapper.writeValueAsString(nodes);
            this.jsonPictures = CommonUtils.objectMapper.writeValueAsString(pictures);
            this.scoreValue = score.getType();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
